/*
 * Circuit3D.h
 *
 *  Created on: Apr 5, 2012
 *      Author: hxu
 */

#ifndef CIRCUIT3D_H_
#define CIRCUIT3D_H_
#include "clockpath.h"
#include <vector>
#include <iostream>
#include "BtreeNode.h"
#include "Vnoise.h"
#include <map>

namespace Cxuh {

class Circuit3D {
public:

	Circuit3D(std::ifstream& ifile, std::fstream& ofile);
	virtual ~Circuit3D();
	void extract_info(std::vector<clockpath>& paths, int lstage, int z);
	BtreeNode* gen_buf_for_path(BtreeNode *rt, clockpath& p1); // generate buffers for path p1 rooting at rt
	void traverse(BtreeNode* rt, std::ostream& out); // traverse and print all nodes

	void extractPar(std::ifstream& fs); // extract parameters from file stream
	//void extractPar(std::ifstream& fs, std::fstream& ofs); // extract parameters from file stream
	void parseLine(std::string& strline);	// parse a line
	//void parseLine(std::string& strline, std::fstream& ofs);	// parse a line and write to ofs

	friend std::ostream& operator<<(std::ostream& out, Circuit3D&);
private:
	std::fstream& ofs;
	int lstage; // unit um
	int srcz; // index of tier of clk src
	BtreeNode *root;
	int num_buf;
	int num_tsv;
	int num_sink;
	int num_mp;

	std::string IFILENAME; // input circuit file
	int TREENODECNT;	// tree node counter
	int TSVBOUND;	// max # of tsv within one tier

	std::vector<Cxuh::clockpath> path_info;	// num_buf for each path in each tier
	std::map<int, Vnoise> vnoie_set; // vnoise in each tier

	void delete_nodes(BtreeNode *tnode); // delete all nodes rooted at tnode
	void update(BtreeNode *rt); // update the info of the circuit
};

} /* namespace Cxuh */
#endif /* CIRCUIT3D_H_ */
